home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1B0L69N (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  10.3 KB  |  396 lines

  1. package com.sun.java.swing.plaf.basic;
  2.  
  3. import com.sun.java.swing.BoxLayout;
  4. import com.sun.java.swing.JComponent;
  5. import com.sun.java.swing.JFrame;
  6. import com.sun.java.swing.JToolBar;
  7. import com.sun.java.swing.LookAndFeel;
  8. import com.sun.java.swing.UIManager;
  9. import com.sun.java.swing.plaf.ComponentUI;
  10. import com.sun.java.swing.plaf.ToolBarUI;
  11. import com.sun.java.swing.plaf.UIResource;
  12. import java.awt.Color;
  13. import java.awt.Component;
  14. import java.awt.Container;
  15. import java.awt.Dimension;
  16. import java.awt.Frame;
  17. import java.awt.Point;
  18. import java.awt.Window;
  19. import java.awt.event.WindowListener;
  20. import java.beans.PropertyChangeEvent;
  21. import java.io.Serializable;
  22.  
  23. public class BasicToolBarUI extends ToolBarUI implements Serializable {
  24.    protected JToolBar toolBar;
  25.    private boolean floating;
  26.    private boolean floatable;
  27.    private int floatingX;
  28.    private int floatingY;
  29.    private JFrame floatingFrame;
  30.    protected DragWindow dragWindow;
  31.    private Container dockingSource;
  32.    private DockingListener dockingListener;
  33.    private int dockingSensitivity = 0;
  34.    protected Color dockingColor = null;
  35.    protected Color floatingColor = null;
  36.    protected Color dockingBorderColor = null;
  37.    protected Color floatingBorderColor = null;
  38.    public static final int HORIZONTAL = 0;
  39.    public static final int VERTICAL = 1;
  40.  
  41.    public boolean canDock(Component c, Point p) {
  42.       boolean b = false;
  43.       if (c.contains(p)) {
  44.          if (this.dockingSensitivity == 0) {
  45.             this.dockingSensitivity = this.toolBar.getSize().height;
  46.          }
  47.  
  48.          if (p.y < this.dockingSensitivity) {
  49.             b = true;
  50.          }
  51.  
  52.          if (p.y > c.getSize().height - this.dockingSensitivity) {
  53.             b = true;
  54.          }
  55.  
  56.          if (p.x < this.dockingSensitivity) {
  57.             b = true;
  58.          }
  59.  
  60.          if (p.x > c.getSize().width - this.dockingSensitivity) {
  61.             b = true;
  62.          }
  63.       }
  64.  
  65.       return b;
  66.    }
  67.  
  68.    protected DockingListener createDockingListener(JToolBar toolbar) {
  69.       return new DockingListener(this, toolbar);
  70.    }
  71.  
  72.    protected DragWindow createDragWindow(JToolBar toolbar) {
  73.       Frame frame = null;
  74.       if (this.toolBar != null) {
  75.          Container p;
  76.          for(p = this.toolBar.getParent(); p != null && !(p instanceof Frame); p = ((Component)p).getParent()) {
  77.          }
  78.  
  79.          if (p != null && p instanceof Frame) {
  80.             frame = (Frame)p;
  81.          }
  82.       }
  83.  
  84.       if (frame == null) {
  85.          this.floatingFrame = this.createFloatingFrame(this.toolBar);
  86.          frame = this.floatingFrame;
  87.       }
  88.  
  89.       DragWindow dragWindow = new DragWindow(frame);
  90.       return dragWindow;
  91.    }
  92.  
  93.    protected JFrame createFloatingFrame(JToolBar toolbar) {
  94.       JFrame frame = new JFrame(((Component)toolbar).getName());
  95.       WindowListener wl = this.createFrameListener();
  96.       ((Window)frame).addWindowListener(wl);
  97.       return frame;
  98.    }
  99.  
  100.    protected WindowListener createFrameListener() {
  101.       return new FrameListener(this);
  102.    }
  103.  
  104.    public static ComponentUI createUI(JComponent x) {
  105.       return new BasicToolBarUI();
  106.    }
  107.  
  108.    protected void dragTo(Point position, Point origin) {
  109.       if (this.toolBar.isFloatable()) {
  110.          if (this.dragWindow == null) {
  111.             this.dragWindow = this.createDragWindow(this.toolBar);
  112.          }
  113.  
  114.          Point offset = this.dragWindow.getOffset();
  115.          if (offset == null) {
  116.             Dimension size = this.toolBar.getPreferredSize();
  117.             offset = new Point(size.width / 2, size.height / 2);
  118.             this.dragWindow.setOffset(offset);
  119.          }
  120.  
  121.          Point global = new Point(origin.x + position.x, origin.y + position.y);
  122.          Point dragPoint = new Point(global.x - offset.x, global.y - offset.y);
  123.          if (this.dockingSource == null) {
  124.             this.dockingSource = this.toolBar.getParent();
  125.          }
  126.  
  127.          Point dockingPosition = this.dockingSource.getLocationOnScreen();
  128.          Point comparisonPoint = new Point(global.x - dockingPosition.x, global.y - dockingPosition.y);
  129.          if (this.canDock(this.dockingSource, comparisonPoint)) {
  130.             this.dragWindow.setBackground(this.getDockingColor());
  131.             String constraint = this.getDockingConstraint(this.dockingSource, comparisonPoint);
  132.             int orientation = this.mapConstraintToOrientation(constraint);
  133.             this.dragWindow.setOrientation(orientation);
  134.             this.dragWindow.setBorderColor(this.dockingBorderColor);
  135.          } else {
  136.             this.dragWindow.setBackground(this.getFloatingColor());
  137.             this.dragWindow.setOrientation(0);
  138.             this.dragWindow.setBorderColor(this.floatingBorderColor);
  139.          }
  140.  
  141.          this.dragWindow.setLocation(dragPoint.x, dragPoint.y);
  142.          if (!this.dragWindow.isVisible()) {
  143.             Dimension size = this.toolBar.getPreferredSize();
  144.             this.dragWindow.setSize(size.width, size.height);
  145.             this.dragWindow.show();
  146.          }
  147.       }
  148.  
  149.    }
  150.  
  151.    protected void floatAt(Point position, Point origin) {
  152.       if (this.toolBar.isFloatable()) {
  153.          Point offset = this.dragWindow.getOffset();
  154.          if (offset == null) {
  155.             offset = position;
  156.             this.dragWindow.setOffset(position);
  157.          }
  158.  
  159.          Point global = new Point(origin.x + position.x, origin.y + position.y);
  160.          this.setFloatingLocation(global.x - offset.x, global.y - offset.y);
  161.          if (this.dockingSource != null) {
  162.             Point dockingPosition = this.dockingSource.getLocationOnScreen();
  163.             Point comparisonPoint = new Point(global.x - dockingPosition.x, global.y - dockingPosition.y);
  164.             if (this.canDock(this.dockingSource, comparisonPoint)) {
  165.                this.setFloating(false, comparisonPoint);
  166.             } else {
  167.                this.setFloating(true, (Point)null);
  168.             }
  169.          } else {
  170.             this.setFloating(true, (Point)null);
  171.          }
  172.  
  173.          this.dragWindow.setOffset((Point)null);
  174.       }
  175.  
  176.    }
  177.  
  178.    public Color getDockingColor() {
  179.       return this.dockingColor;
  180.    }
  181.  
  182.    private String getDockingConstraint(Component c, Point p) {
  183.       String s = "North";
  184.       if (p != null && c.contains(p)) {
  185.          if (this.dockingSensitivity == 0) {
  186.             this.dockingSensitivity = this.toolBar.getSize().height;
  187.          }
  188.  
  189.          if (p.y > c.getSize().height - this.dockingSensitivity) {
  190.             s = "South";
  191.          }
  192.  
  193.          if (p.x < this.dockingSensitivity) {
  194.             s = "West";
  195.          }
  196.  
  197.          if (p.x > c.getSize().width - this.dockingSensitivity) {
  198.             s = "East";
  199.          }
  200.  
  201.          if (p.y < this.dockingSensitivity) {
  202.             s = "North";
  203.          }
  204.       }
  205.  
  206.       return s;
  207.    }
  208.  
  209.    public Color getFloatingColor() {
  210.       return this.floatingColor;
  211.    }
  212.  
  213.    public Dimension getMaximumSize(JComponent c) {
  214.       return this.getPreferredSize(c);
  215.    }
  216.  
  217.    public Dimension getMinimumSize(JComponent c) {
  218.       return this.getPreferredSize(c);
  219.    }
  220.  
  221.    public Dimension getPreferredSize(JComponent c) {
  222.       return null;
  223.    }
  224.  
  225.    protected void installDefaults(JComponent c) {
  226.       LookAndFeel.installBorder(c, "ToolBar.border");
  227.       LookAndFeel.installColorsAndFont(c, "ToolBar.background", "ToolBar.foreground", "ToolBar.font");
  228.       if (this.dockingColor == null || this.dockingColor instanceof UIResource) {
  229.          this.dockingColor = UIManager.getColor("ToolBar.dockingColor");
  230.       }
  231.  
  232.       if (this.floatingColor == null || this.floatingColor instanceof UIResource) {
  233.          this.floatingColor = UIManager.getColor("ToolBar.floatingColor");
  234.       }
  235.  
  236.       if (this.dockingBorderColor == null || this.dockingBorderColor instanceof UIResource) {
  237.          this.dockingBorderColor = UIManager.getColor("ToolBar.dockingBorderColor");
  238.       }
  239.  
  240.       if (this.floatingBorderColor == null || this.floatingBorderColor instanceof UIResource) {
  241.          this.floatingBorderColor = UIManager.getColor("ToolBar.floatingBorderColor");
  242.       }
  243.  
  244.    }
  245.  
  246.    protected void installListeners(JComponent c) {
  247.       this.dockingListener = this.createDockingListener((JToolBar)c);
  248.       this.setFloatable(true);
  249.    }
  250.  
  251.    public void installUI(JComponent c) {
  252.       this.toolBar = (JToolBar)c;
  253.       this.installDefaults(c);
  254.       this.dockingSensitivity = 0;
  255.       this.floating = this.floatable = false;
  256.       this.floatingX = this.floatingY = 0;
  257.       this.floatingFrame = this.createFloatingFrame(this.toolBar);
  258.       this.setOrientation(0);
  259.       this.installListeners(c);
  260.       c.setOpaque(true);
  261.    }
  262.  
  263.    public boolean isFloating() {
  264.       return this.floating;
  265.    }
  266.  
  267.    private int mapConstraintToOrientation(String constraint) {
  268.       int orientation = 0;
  269.       if (constraint != null && (constraint.equals("East") || constraint.equals("West"))) {
  270.          orientation = 1;
  271.       }
  272.  
  273.       return orientation;
  274.    }
  275.  
  276.    public void propertyChange(PropertyChangeEvent e) {
  277.       e.getPropertyName();
  278.       if (e.getPropertyName().equals("floatable")) {
  279.          Boolean b = (Boolean)e.getNewValue();
  280.          this.setFloatable(b);
  281.       }
  282.  
  283.    }
  284.  
  285.    public void setDockingColor(Color c) {
  286.       this.dockingColor = c;
  287.    }
  288.  
  289.    public void setFloatable(boolean b) {
  290.       if (b) {
  291.          this.toolBar.addMouseMotionListener(this.dockingListener);
  292.          this.toolBar.addMouseListener(this.dockingListener);
  293.       } else {
  294.          this.toolBar.removeMouseMotionListener(this.dockingListener);
  295.          this.toolBar.removeMouseListener(this.dockingListener);
  296.       }
  297.  
  298.    }
  299.  
  300.    public void setFloating(boolean b, Point p) {
  301.       if (this.toolBar.isFloatable()) {
  302.          if (this.dragWindow != null) {
  303.             this.dragWindow.setVisible(false);
  304.          }
  305.  
  306.          this.floating = b;
  307.          if (b) {
  308.             if (this.dockingSource == null) {
  309.                this.dockingSource = this.toolBar.getParent();
  310.                this.dockingSource.remove(this.toolBar);
  311.             }
  312.  
  313.             if (this.floatingFrame == null) {
  314.                this.floatingFrame = this.createFloatingFrame(this.toolBar);
  315.             }
  316.  
  317.             this.floatingFrame.getContentPane().add(this.toolBar, "Center");
  318.             this.setOrientation(0);
  319.             this.floatingFrame.pack();
  320.             this.floatingFrame.setLocation(this.floatingX, this.floatingY);
  321.             this.floatingFrame.show();
  322.          } else {
  323.             this.floatingFrame.setVisible(false);
  324.             this.floatingFrame.getContentPane().remove(this.toolBar);
  325.             String constraint = this.getDockingConstraint(this.dockingSource, p);
  326.             int orientation = this.mapConstraintToOrientation(constraint);
  327.             this.setOrientation(orientation);
  328.             if (this.dockingSource == null) {
  329.                this.dockingSource = this.toolBar.getParent();
  330.             }
  331.  
  332.             this.dockingSource.add(constraint, this.toolBar);
  333.          }
  334.  
  335.          this.dockingSource.invalidate();
  336.          Container dockingSourceParent = this.dockingSource.getParent();
  337.          if (dockingSourceParent != null) {
  338.             dockingSourceParent.validate();
  339.          }
  340.  
  341.          this.dockingSource.repaint();
  342.       }
  343.  
  344.    }
  345.  
  346.    public void setFloatingColor(Color c) {
  347.       this.floatingColor = c;
  348.    }
  349.  
  350.    public void setFloatingLocation(int x, int y) {
  351.       this.floatingX = x;
  352.       this.floatingY = y;
  353.    }
  354.  
  355.    public void setOrientation(int orientation) {
  356.       if (orientation == 1) {
  357.          this.toolBar.setLayout(new BoxLayout(this.toolBar, 1));
  358.       } else {
  359.          this.toolBar.setLayout(new BoxLayout(this.toolBar, 0));
  360.       }
  361.  
  362.       if (this.dragWindow != null) {
  363.          this.dragWindow.setOrientation(orientation);
  364.       }
  365.  
  366.    }
  367.  
  368.    protected void uninstallDefaults(JComponent c) {
  369.       LookAndFeel.uninstallBorder(c);
  370.       this.dockingColor = null;
  371.       this.floatingColor = null;
  372.       this.dockingBorderColor = null;
  373.       this.floatingBorderColor = null;
  374.    }
  375.  
  376.    protected void uninstallListeners(JComponent c) {
  377.       this.setFloatable(false);
  378.       if (this.dockingListener != null) {
  379.          this.dockingListener = null;
  380.       }
  381.  
  382.    }
  383.  
  384.    public void uninstallUI(JComponent c) {
  385.       this.uninstallDefaults(c);
  386.       if (this.isFloating()) {
  387.          this.setFloating(false, (Point)null);
  388.       }
  389.  
  390.       this.floatingFrame = null;
  391.       this.dragWindow = null;
  392.       this.dockingSource = null;
  393.       this.uninstallListeners(c);
  394.    }
  395. }
  396.